package cn.jdy.task.service.classify;


import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import cn.jdy.authority.entity.Organ;
import cn.jdy.authority.service.OrganService;
import cn.jdy.common.util.XMLUtil;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.framework.util.DateUtil;
import cn.jdy.framework.util.StringUtil;
import cn.jdy.plugins.entity.ApplyCash;
import cn.jdy.plugins.entity.Maid;
import cn.jdy.plugins.entity.Member;
import cn.jdy.plugins.service.ApplyCashService;
import cn.jdy.plugins.service.CRateOrderService;
import cn.jdy.plugins.service.LiveService;
import cn.jdy.plugins.service.MaidService;
import cn.jdy.plugins.service.MemberService;
import cn.jdy.plugins.service.PointRecordService;
import cn.jdy.plugins.service.ProductxService;
import cn.jdy.plugins.service.WatchUserService;
import cn.jdy.plugins.service.WebsiteService;
import cn.jdy.plugins.wap.util.ChatUtil;
import cn.jdy.plugins.wap.util.HttpUtils;
import cn.jdy.plugins.wap.util.RedPacketUtil;
import cn.jdy.task.service.TaskGroup;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;




@TaskGroup("加权分红")
public class Websitejqfh implements Job {
	@Autowired
	private WebsiteService websiteService;
	@Autowired
	private CRateOrderService cRateOrderService;
	@Autowired
	private ProductxService productxService;
	@Autowired
	private MemberService memberService;
	@Autowired
	private PointRecordService pointRecordService;
	@Autowired
	private ApplyCashService applyCashService;
	@Autowired
	private WatchUserService watchUserService;

	@Autowired
	private OrganService organService;
	@Autowired
	private LiveService liveService;
	@Autowired
	private MaidService maidService;
	
	
	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		try {
			System.out.println("执行了一次");
		
			DetachedCriteria dc = DetachedCriteria.forClass(Member.class);

				dc.add(Restrictions.or(Restrictions.eq("userLevel", "0")));
				dc.add(Restrictions.or(Restrictions.eq("userLevel", "5")));
				dc.add(Restrictions.or(Restrictions.eq("userLevel", "6")));
				dc.add(Restrictions.or(Restrictions.eq("userLevel", "7")));
				dc.add(Restrictions.or(Restrictions.eq("userLevel", "8")));
				dc.add(Restrictions.or(Restrictions.eq("userLevel", "9")));
				List<Member> list =	memberService.find(dc);
	
				for (Member mb : list) {
					double dw=memberService.findcheckqquan(mb.getId())==null?0:memberService.findcheckqquan(mb.getId());
					if(dw>1){
					//用户等级   0运营商，1超级会员，2普通会员,3 初级代理，4 高级代理，5是1星运营商，6是2星运营商，7是3星运营商,8是4星运营商,9是5星运营商
					List<Member> mlist3=memberService.find("fid",mb.getId(),"userLevel","3");//低级
					List<Member> mlist4=memberService.find("fid",mb.getId(),"userLevel","4"); //高级
					
					//算总代权重
					int gqz=mlist4.size()*3+mlist3.size();
					
						for (Member member : mlist4) {
								Maid  ma3=new Maid();
			  		  	  		ma3.setOrderNo("3");
					  		  	ma3.setUsername(member.getUserName());
					  		  	ma3.setMch_id("3");
					  		  	ma3.setMemberid(mb.getId());
					  			ma3.setProductxid("3");
					  			ma3.setProductxname("运营商给直推分钱");
					  			ma3.setCreateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
					  			ma3.setTotal(dw);
			  		  	  		ma3.setWermemberid(member.getId());
					  		  	ma3.setWerusername(member.getUserName());
					  		  	ma3.setIsfa("0");
					  		  	double de=dw*(3/gqz);
					  		  	ma3.setFendtotal(de);
		  		  	  	  		maidService.save(ma3);
		  		  	  	  		member.setMoney(member.getMoney()+de);
		  		  	  	  		member.setKtxmoney(member.getKtxmoney()+de);
		  				  		memberService.update(member);
						}
						
						for (Member member : mlist3) {
							Maid  ma3=new Maid();
		  		  	  		ma3.setOrderNo("3");
				  		  	ma3.setUsername(member.getUserName());
				  		  	ma3.setMch_id("3");
				  		  	ma3.setMemberid(mb.getId());
				  			ma3.setProductxid("3");
				  			ma3.setProductxname("运营商给直推分钱");
				  			ma3.setCreateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
				  			ma3.setTotal(dw);
		  		  	  		ma3.setWermemberid(member.getId());
				  		  	ma3.setWerusername(member.getUserName());
				  		  	ma3.setIsfa("0");
				  		  	double de=dw*(1/gqz);
				  		  	ma3.setFendtotal(de);
	  		  	  	  		maidService.save(ma3);
	  		  	  	  		member.setMoney(member.getMoney()+de);
	  		  	  	  		member.setKtxmoney(member.getKtxmoney()+de);
	  				  		memberService.update(member);
					}
				}
			  }
			//查询所有用户
			
		} catch(Exception e) {
			BaseLog.e(this.getClass(), "invoke", e);
		}
	}
	
	
	
public static void main(String[] args) throws ParseException {
 
} 
}
